package com.jwdf.uda.ui.fragment.search;

import android.os.Bundle;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.GuideEvent;
import com.jwdf.uda.event.MessageNoticeEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.SearchAction;
import com.jwdf.uda.ui.adapter.DesignerAdapter;
import com.jwdf.uda.ui.adapter.item.UserSimpleItem;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/20.
 */
public class SearchDesignerResultsFragment extends SimpleListFragment {
    private String mContent;
    private  PbBean.PbSignSearchType type;

    private DesignerAdapter mAdapter;

    public static SearchDesignerResultsFragment instance(String content,PbBean.PbSignSearchType type) {
        SearchDesignerResultsFragment view = new SearchDesignerResultsFragment();
        Bundle args = new Bundle();
        args.putString("content", content);
        args.putSerializable("type",type);
        view.setArguments(args);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContent = getArguments().getString("content", "");
        type=(PbBean.PbSignSearchType)getArguments().getSerializable("type");
    }
    @Override
    protected boolean canPullRefresh() {
        return true;
    }
    @Override
    protected void initViews() {
        mAdapter = new DesignerAdapter(getContext());
        mAdapter.setCurrentUser(true);
        mDataBinding.commonFragmentList.setPullEnable(true);
        mDataBinding.commonFragmentList.setAdapter(mAdapter);
    }
    @Override
    protected void onListItemClick(int position) {
        PbBean.PbUserSimple user = mAdapter.getItem(position).userSimple;
        EventBusUtil.post(new GuideEvent(GuideEvent.EVENT_GUIDE_CHOOSE,user));
        getActivity().finish();
    }
    @Override
    protected void requestData(final boolean refresh) {
        if(refresh && mAdapter.isEmpty()) {
            showProgressDialog();
        }
        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(SearchAction.newInstance(getContext(), mContent, offset, type)
                                 .request(new AHttpService.IResCallback<PbBean.PbResPageSearch>() {
                                     @Override
                                     public void onCallback(int resultCode, Response<PbBean.PbResPageSearch> response, PbBean.PbBaseServerInfo
                                                                                                                                                         baseServerInfo) {
                                         if(getView() != null) {
                                             closeProgressDialog();
                                             if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                                                 mDataBinding.commonFragmentList.stopRefresh();
                                                 mDataBinding.commonFragmentList.stopLoadMore();
                                                 return;
                                             }
                                             doSuccessResponse(refresh, response.body());
                                         }
                                     }
                                 }));
    }

    private void refreshUserInfo() {
        requestData(true);
        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.REFRESH_USER_INFO));
    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResPageSearch body) {
        if (body != null) {
            if (body.getDesignerListList().isEmpty()) {
                mDataBinding.tvEmptyData.setText("对不起，未找到符合条件的用户");
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
            } else {
                if (refresh) {
                    mAdapter.setData(UserSimpleItem.convert(body.getDesignerListList()));
                } else {
                    mAdapter.append(UserSimpleItem.convert(body.getDesignerListList()));
                }
            }
            mDataBinding.commonFragmentList.setPullLoadEnable(body.getDesignerListList().size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
}
